/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
  function getRomanValue(char) {
    switch(char) {
      case 'I':
        return 1;
      case 'V':
        return 5;
      case 'X':
        return 10;
      case 'L':
        return 50;
      case 'C':
        return 100;
      case 'D':
        return 500;
      case 'M':
        return 1000;
    }
  }
  let lastTag = getRomanValue(s[0]), result = 0, tempResult = getRomanValue(s[0]);
  if(s.length === 1)
    return tempResult;
  for(let i = 1; i < s.length; i++) {
    let tag = getRomanValue(s[i]);
    if(lastTag != tag) {
      if(lastTag < tag) {
        // 如果上一个标记比当前标记的值要小，则得到的结果应为负数
        // console.log(`${result}减${tempResult}`);
        result -= tempResult;
      } else {
        // 如果上一个标记比当前标记的值要大，则可以直接将临时的结果加上
        // console.log(`${result}加${tempResult}`);
        result += tempResult;
      }
      lastTag = tag;
      tempResult = tag;
    } else {
      tempResult += tag;
    }
    if(i == s.length - 1) {
      result += tempResult;
      // console.log(`${result}加${tempResult}`);
    }
  }
  return result;


};
let s = "III";
console.log(romanToInt(s));
s = "IV";
console.log(romanToInt(s));
s = "IX";
console.log(romanToInt(s));
s = "LVIII";
console.log(romanToInt(s));
s = "MCMXCIV";
console.log(romanToInt(s));
s = "D";
console.log(romanToInt(s));
